<?php
namespace App\Http\Admin\ProcessProduct\Controllers;

use App\Http\Admin\ProcessProduct\Models\ProcessProduct;
use Framework\BaseClass\Http\Admin\Controller;
use App\Eloquent\Ygt\ProcessProduct as ProcessProductModel;
use \App\Eloquent\Ygt\ProcessProductCodeSet;


class CodeSetController extends Controller
{
    protected $company = false;

    public function __construct()
    {
        parent::__construct();
    }

    protected $rules = [
        'content' => 'bail|required|string',
        'process_product_id' => 'required|integer',
    ];

    public function add(){
        $param = request()->all();
        if (request()->isMethod('post')) {
            $result = $this->validateRequestParameters($param, $this->rules, false);

            if ($result) {
                return $this->ajaxFail($result);
            }

            //添加
            $data = [
                'process_product_id'=>$param['process_product_id'],
                'content'=>$param['content'],
            ];
            $id = ProcessProductCodeSet::insertOneData($data);

            return $this->ajaxSuccess();
        }
        else{
            $process_product_id = $param['process_product_id'];
            $process_product_info = ProcessProduct::where('id','=',$process_product_id)->first()->toArray();
            $process_product_name = $process_product_info['title'];

            return $this->view('add',compact('process_product_id','process_product_name'));
        }
    }



    public function edit(){
        $admin_user         = request()->user('admin')->toArray();
        $company_id         = $admin_user['company_id'];

        $params             = $this->requestJson();
        $id                 = isset($params['id']) ? $params['id'] : 0;

        $result = $this->validateRequestParameters($params, $this->rules, false);

        if ($result) {
            return $this->ajaxFail($result);
        }

        if(!$id){
            return $this->ajaxFail($result);
        }

        $data               = [
            'content'=>$params['content'],
        ];

        if($id){
            $where          = ['id'=>$id];
            $result         = ProcessProductCodeSet::updateOneData( $where, $data );
        }

        if( $result ){
            return $this->ajaxSuccess( '成功' );
        } else {
            return $this->ajaxFail( '失败' );
        }
    }

    public function delete()
    {
        $admin_user         = request()->user('admin')->toArray();
        $company_id         = $admin_user['company_id'];

        $id                 = request('id',0);
        if($id){
            $where          = ['id'=>$id];
            $del_result     = ProcessProductCodeSet::del($where);
            if( $del_result !== false ){
                return $this->ajaxSuccess('成功');
            }
            else{
                return $this->ajaxFail('失败');
            }
        } else {
            return $this->ajaxFail('失败');
        }
    }


    public function processProductLists(){
        $adminUser         = request()->user('admin')->toArray();
        $companyId         = $adminUser['company_id'];

        if (request()->isMethod('post')) {
            $keyword          = request('keyword');

            $obj = new ProcessProductModel();
            if(!empty($keyword)){
                $obj = $obj->where('title','like','%'.$keyword.'%');
            }

            $list = $obj->get();
            return ['list'=>$list,'pageSize'=>10000];
        }
        else{
            $process_product_id           = request('process_product_id',0);
            $id = request('id',0);

            //获得材料列表
            $list = ProcessProductModel::get()->toArray();
            $data = ['id'=>$id,'process_product_id'=>$process_product_id,'list'=>$list];
        }

        return $this->view('product-list-for-select',$data);
    }

    public function lists(){
        $adminUser         = request()->user('admin')->toArray();
        $companyId         = $adminUser['company_id'];

        if (request()->isMethod('post')) {
            $content         = request( 'content', '' );

            $process_product_id = request('process_product_id',0);

            $obj = new ProcessProductCodeSet();
            $obj = $obj->where('process_product_id','=',$process_product_id);

            if(!empty($content)){
                $obj = $obj->where("ygt_process_product_code_set.content",'=',$content);
            }

            $fields = [
                'ygt_process_product_code_set.id',
                'ygt_process_product_code_set.process_product_id',
                'ygt_process_product_code_set.content',
                'ygt_process_product.title'
            ];

            $lists = $obj->leftJoin('ygt_process_product','ygt_process_product.id','=','ygt_process_product_code_set.process_product_id')
                ->orderBy('id','desc')
                ->get($fields)->toArray();
            $result         = collect($lists)->toJson();
            return $result;
        } else {
            $process_product_id = request('process_product_id',0);
            $obj = new ProcessProductCodeSet();
            $obj = $obj->where('ygt_process_product_code_set.process_product_id','=',$process_product_id);

            $fields = [
                'ygt_process_product_code_set.id',
                'ygt_process_product_code_set.content',
                'ygt_process_product.title'
            ];

            $obj->leftJoin('ygt_process_product','ygt_process_product_code_set.process_product_id','=','ygt_process_product.id')
                ->orderBy('id','desc')
                ->get($fields)->toArray();

            //$data['process_product_id'] = $process_product_id;
            //$process_product_id = json_encode($data);

            return $this->view('lists', compact('process_product_id') );
        }
    }

}